<?php
/**
 * 投注中奖执行脚本
 */
include_once 'common.php';
include_once 'winner_func.php';
echo '<pre>';
$mainTab = $dbprefix . 'lottery_order_main';
$itemTab = $dbprefix . 'lottery_order_item';
$perTab  = $dbprefix . 'lottery_k3';

// 获取所有投注未开奖信息，准备计算中奖状态
$sql  = 'SELECT item.`id`, item.`o_id`, item.`cn_play`, item.`en_play`, item.`betting_no`,
                item.`num`, item.`multiple`, item.`money_unit`, item.`eo_money`, item.`odds`,
                main.`uid`, main.`order_no`, main.`lott_id`,
                per.`code` as open_code
        FROM ' . $itemTab . ' AS item
        LEFT JOIN ' . $mainTab . ' AS main
        ON item.`o_id` = main.`id`
        LEFT JOIN ' . $perTab . ' AS per
        ON item.`period_id` = per.`id`
        WHERE item.`winner` = 0 AND per.`code` != "" AND main.`lott_id` != "" ORDER BY item.`id` DESC limit 3000';
$list = getList($sql, $db);
if (!$list){
    echo 'NO Data';
    return;
}
// 查询所有彩种信息， 以便检查不同类型的投注来调用不同的计算中奖方法
$sql = 'SELECT cate.`sname`, lott.`id`, lott.`title` FROM ' . $dbprefix . 'lottery AS lott
        LEFT JOIN ' . $dbprefix . 'lottery_category AS cate
        ON lott.`cate_id` = cate.`id`';
$lottList  = getList($sql, $db);
$lottIdKey = array();
foreach ($lottList as $k => $v){
    $lottIdKey[$v['id']] = $v;
}
// 准备开始
$update = array();
foreach ($list as $k => $v){
    if (!$v['lott_id']) continue;
    $lottTypeKey       = $lottIdKey[$v['lott_id']]['sname'];
    $code_arr          = explode(',', $v['open_code']);
    if ($lottTypeKey == 'k3'){
        $update = winner_K3($v, $code_arr, $db);
    }elseif ($lottTypeKey == 'ssc'){
        $update = winner_SSC($v, $code_arr, $db);
    }elseif ($lottTypeKey == 'syxw'){
        $update = winner_SYXW($v, $code_arr, $db);
    }
    $sql  = 'UPDATE ' . $itemTab . ' SET ' . CreateUpdateSql($update) . ' WHERE id = ' . $v['id'];
    $flag = $db->exec($sql);
    
    if ($flag){
        echo 'success[' . $update['winner'] . ']<br/>';
    }else{
        echo 'error[' . $v['en_play'] . ':' . $v['betting_no'] . ']<br/>';
    }
}
echo 'end';


